﻿#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

using octalforty.Chinchillin.Shared.Deployment.Agent;
using octalforty.Chinchillin.Shared.Deployment.Resources;

namespace octalforty.Chinchillin.Console.Deployment
{
    public class DeploymentCallback : IDeploymentCallback
    {
        private int indentLevel;

        public IResourceProvider ResourceProvider
        { get; set; }

        #region IDeploymentCallback Members
        public byte[] GetResourceChecksum(Uri resourceUri)
        {
            return ResourceProvider.GetResourceChecksum(resourceUri);
        }

        public ResourceInfo[] GetResourcesChecksums(Uri[] resourcesUris)
        {
            var resourceInfos = resourcesUris.Select(u => new ResourceInfo(u, GetResourceChecksum(u)));
            return resourceInfos.ToArray();
        }

        public Stream GetResourceStream(Uri resourceUri)
        {
            return ResourceProvider.GetResourceStream(resourceUri);
        }

        public Uri[] GetResourcesHierarchy(Uri baseResourceUri)
        {
            return ResourceProvider.GetResourcesHierarchy(baseResourceUri);
        }

        public void TraceEvent(string serverName, DateTime serverTimestamp, TraceLevel traceLevel, TraceEventType eventType, int eventCode, string message)
        {
            if(eventType == TraceEventType.Stop && indentLevel > 0)
                indentLevel -= 4;

            using(new ConsoleStylingScope(GetTraceEventColor(eventType)))
                System.Console.WriteLine("{0}: [{1:HH:mm:ss.fff}] - {2}{3}", serverName, serverTimestamp, new string(' ', indentLevel), message);   

            if(eventType == TraceEventType.Start)
                indentLevel += 4;
        }

        public void TraceEvent(string serverName, DateTime serverTimestamp, TraceEventType eventType, int eventCode, string eventMessage)
        {
            TraceEvent(serverName, serverTimestamp, TraceLevel.Info, eventType, eventCode, eventMessage);
        }
        #endregion

        private static ConsoleColor? GetTraceEventColor(TraceEventType eventType)
        {
            switch(eventType)
            {
                case TraceEventType.Critical:
                case TraceEventType.Error:
                    return ConsoleColor.Red;
                case TraceEventType.Warning:
                    return ConsoleColor.Yellow;
                case TraceEventType.Information:
                case TraceEventType.Start:
                case TraceEventType.Stop:
                    return ConsoleColor.Green;
                case TraceEventType.Verbose:
                case TraceEventType.Suspend:
                case TraceEventType.Resume:
                case TraceEventType.Transfer:
                    return ConsoleColor.Gray;
                default:
                    throw new ArgumentOutOfRangeException("eventType");
            } // switch
        }
    }
}
